SUB Statement ---------------------------------------------------------------------------- Action Declares the name and the parameters of a SUB procedure. Syntax SUB globalname( parameterlist) STATIC statementblock EXIT SUB statementblock END SUB Remarks The SUB statement uses the following arguments. ----------------------------------------------------------------------------- Argument Description ---------------------------------------------------------------------------- globalname A variable name up to 40 characters long. This name cannot appear in any other SUB or FUNCTION statement in the same program or the user library. The name cannot include a type-declaration character ( %, &, !, #, @, or $). parameterlist The list of variables, representing parameters, that will be passed to the SUB procedure when it is called. Multiple variables are separated by commas. Parameters are passed by reference, Argument Description ---------------------------------------------------------------------------- Parameters are passed by reference, so any change to a parameter's value inside the SUB procedure changes its value in the calling program. STATIC Indicates that the SUB procedure's local variables are to be saved between calls. Without STATIC, the local variables are allocated each time the SUB procedure is invoked, and the variables' values are lost when the SUB returns to the calling program. The STATIC attribute does not affect variables that are used in a SUB procedure but declared outside the procedure in DIM or COMMON statements using Argument Description ---------------------------------------------------------------------------- DIM or COMMON statements using the SHARED statement. EXIT Causes an immediate exit from a SUB. Program execution continues with the statement after the CALL statement. A SUB parameterlist argument has the following syntax. variable( ) AS type , variable( ) AS type... ----------------------------------------------------------------------------- Argument Description ---------------------------------------------------------------------------- variable A BASIC variable name. Previous Argument Description ---------------------------------------------------------------------------- variable A BASIC variable name. Previous versions of BASIC required the number of dimensions in parentheses after an array name. In the current version of BASIC, the number of dimensions is not required. AS type The type of the variable. INTEGER, LONG, SINGLE, DOUBLE, CURRENCY, STRING, or a user-defined type. You cannot use a fixed-length string, or an array of fixed-length strings, as a parameter. However, you can use a simple fixed-length string as an argument in a CALL statement; BASIC converts a simple fixed-length string argument to a Argument Description ---------------------------------------------------------------------------- fixed-length string argument to a variable-length string argument before passing the string to a SUB procedure. A SUB procedure is a separate procedure, like a FUNCTION procedure. However, unlike a FUNCTION procedure, a SUB procedure cannot be used in an expression. SUB and END SUB mark the beginning and end of a SUB procedure. You also can use the optional EXIT SUB statement to exit a SUB procedure. SUB procedures are called by a CALL statement or by using the procedure name followed by the argument list. See the entry for the CALL statement. BASIC SUB procedures can be recursive -- they can call themselves to perform a given task. See the second example below. The STATIC attribute indicates that all variables local to the SUB procedure are static -- their values are saved between calls. Using the STATIC keyword increases execution speed slightly. STATIC usually is not used with recursive SUB procedures. Any SUB procedure variables or arrays are considered local to that SUB procedure, unless they are explicitly declared as shared variables in a SHARED statement. You cannot define SUB procedures, DEF FN functions, or FUNCTION procedures inside a SUB procedure. Note You cannot use GOSUB, GOTO, or RETURN to enter or exit a SUB procedure. See Also CALL (BASIC), DECLARE (BASIC), SHARED, STATIC Statement Examples See the CALL (BASIC) statement programming example, which uses the SUB statement.